(function(){
	if(!window.cell) window.cell={};
	if(!window.cell.tab) window.cell.tab={};
	if(!window.cell.console) window.cell.console={
		overwrite: true,
		svcConsole: Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService),
		log:function(msg){
			this.svcConsole.logStringMessage(msg);
		},
	};
	if(!window.$t) window.$t = cell.tab = function(key) {
		if ( window == this || !this.init ){
			return new cell.tab(key);
		}
		return this.init(key);
	}
	cell.tab.fn = cell.tab.prototype = {
		version: "2.1.4",
		svcSessionStore:Components.classes["@mozilla.org/browser/sessionstore;1"].getService(Components.interfaces.nsISessionStore),
		svcWindow: Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator),
		svcFavicon: Components.classes["@mozilla.org/browser/favicon-service;1"].getService(Components.interfaces.nsIFaviconService),
		svcURI:function(uri){
			return Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).newURI(uri, null, null)
		},
		init:function(key){
			if(typeof(key)=="string"){
				return this.getTabById(key);
			}
			var gBrowser=this.svcWindow.getMostRecentWindow('navigator:browser').getBrowser();
			for (var i=0;i < gBrowser.mTabContainer.childNodes.length;i++){
//				this["url"][i]=gBrowser.getBrowserAtIndex(i).currentURI.spec; // for Fx 3.5
				this[i] = gBrowser.mTabContainer.childNodes[i];
			}
			this.length=gBrowser.mTabContainer.childNodes.length;
			return this;
		},
		favicon:function(i) {
			if(!i) i=0;
			try{
				var favicon=this.svcFavicon.getFaviconForPage(this.svcURI(this.url(i)));
			}catch(err){
				var favicon=this.svcFavicon.defaultFavicon;
			}
			return favicon.spec;
		},
		remove:function(i){
			if(!i) var i=0;
			var gBrowser=this.svcWindow.getMostRecentWindow('navigator:browser').getBrowser();
			gBrowser.removeTab(this[i]);
		},
		label:function(i) {
			if(!i) var i=0;
			return this[i].getAttribute("label");
		},
		url:function(i) {
			if(!i) var i=0;
			var gBrowser=this.svcWindow.getMostRecentWindow('navigator:browser').getBrowser();
			return gBrowser.getBrowserAtIndex(i).currentURI.spec;
//				this["url"][i]=gBrowser.getBrowserAtIndex(i).currentURI.spec; // for Fx 3.5
			//return this["url"][i];
		},
		linkedpanel:function(i) {
			if(!i) var i=0;
			return this[i].getAttribute("linkedpanel");
		},
		select:function(){
		//	var objTab=$tab2.get(id);
			var gBrowser=this.svcWindow.getMostRecentWindow('navigator:browser').getBrowser();
			gBrowser.selectedTab =this[0];
			gBrowser.focus();
			return;
		},
		selected:function(i) {
			if(!i) var i=0;
			return this[i].getAttribute("selected");
		},
		getTabById:function(id) {
			var gBrowser=this.svcWindow.getMostRecentWindow('navigator:browser').getBrowser();
			for (var i=0;i < gBrowser.mTabContainer.childNodes.length;i++){
				var currentTab = gBrowser.mTabContainer.childNodes[i];
				if(currentTab.getAttribute("selected")=="true" && !id){
					this[0]=currentTab;
					this.length=1;
					return this;
				}
				if(currentTab.getAttribute("linkedpanel")==id){
					this[0]=currentTab;
					this.length=1;
					return this;
				}
			}
			return false;
		},
		thumbnail:function(i,arg){
			if(!i) var i=0;
			var browser=this.svcWindow.getMostRecentWindow('navigator:browser').getBrowser().getBrowserForTab(this[i])
			var content = browser.contentWindow;
			var doc= browser.contentDocument;
			
			var left=0;
			var w=content.innerWidth;
			var canvas = document.createElementNS("http://www.w3.org/1999/xhtml","canvas");

			if(!arg) var arg={};
			if(arg.autofit==true) {
				if(doc.getElementsByTagName("body").length!=0){
					var left=doc.getElementsByTagName("body")[0].firstElementChild.offsetLeft;
					var width=width-(left*2);
				}
			}
			if(!arg.background) arg.background="rgb(255,255,255)";
			if(!arg.width) arg.width=(window.screen.width/6);
			if(!arg.height) arg.height=(window.screen.height/6);
			if(!arg.maxwidth) arg.maxwidth=(window.screen.width/6);
			if(!arg.maxheight) arg.maxheight=(window.screen.height/6);
			
			canvas.style.width=arg.width+"px";
			canvas.style.height=arg.height+"px";
/*			canvas.style.maxWidth=arg.maxwidth+"px";
			canvas.style.maxHeight=arg.maxheight+"px";*/
			canvas.width=arg.width;
			canvas.height=arg.height;
			
			if(!arg.scale) arg.scale=canvas.width / content.innerWidth;
			var h=canvas.height / arg.scale;

			var ctx = canvas.getContext("2d");
			ctx.clearRect(0, 0, canvas.width, canvas.height);
			ctx.save();
			ctx.scale(arg.scale, arg.scale);

			ctx.drawWindow(content, left, 0, content.innerWidth + content.scrollX , h + content.scrollY, arg.background);
			ctx.restore();
			
			return canvas;
		},
		clone:function(i){
			if(!i) i=0;
			this.svcSessionStore.duplicateTab(window, this[i]);
		},
		open:function(url,arg,callback){
			if(!url) var url="about:blank";
			if(!arg) var arg={};

			var tab = this.svcWindow.getMostRecentWindow('navigator:browser').getBrowser().getBrowserForTab(this[0]);
			var instance = this.svcWindow.getEnumerator('navigator:browser').getNext().getBrowser();
			if(!arg.selected){}else{ 
				this.svcWindow.getMostRecentWindow('navigator:browser').getBrowser().selectedTab=this[0];
			}
			tab.loadURI(url);
		},

		log:function(str) {cell.console.log(str);},
	}
})();